package com.bestv.search.engine.servlet;

import com.bestv.search.engine.command.SuggestCommand;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;

public class SugIndexServlet extends HttpServlet {
    private static final long serialVersionUID = 7356248208047099050L;
    protected final Log logger = LogFactory.getLog(getClass());
    private SuggestCommand suggestCommand;

    public SugIndexServlet() {
        super();
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,
            IOException {
        doExecute(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doExecute(request, response);
    }

    public void destroy() {
        super.destroy();
    }

    private void doExecute(HttpServletRequest request, HttpServletResponse response) throws ServletException,
            IOException {
        logger.info("Start suggestion index @" + new Date());
        suggestCommand.createOrUpdateIndex();
        logger.info("End suggestion index @" + new Date());

        response.getWriter().write("Suggestion index completed!");
    }

    public SuggestCommand getSuggestCommand() {
        return suggestCommand;
    }

    public void setSuggestCommand(SuggestCommand suggestCommand) {
        this.suggestCommand = suggestCommand;
    }
}
